/* eslint-disable no-console */
/* eslint-disable import/prefer-default-export */
import { wsInstance } from '../core/TioConnection';
import { TioCommand } from '../types';

interface WsMessage {
  cmd: TioCommand;
  data: unknown;
}

export function tioSend(cmd: TioCommand, data: unknown) {
  const connection = wsInstance;
  if (!connection || !wsInstance.ws || connection.ws.readyState !== WebSocket.OPEN) {
    console.error('WebSocket connection not available');
    return;
  }

  const message: WsMessage = { cmd, data };
  connection.ws.send(JSON.stringify(message));
}
